package cn.wolfcode.web.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.PermissionAnnotation;
import cn.wolfcode.util.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/frontend")
public class IndexController {

    @Autowired
    private IBusinessService service;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IAppointmentService appointmentService;

    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("business",service.getOne());
        model.addAttribute("businessAll",service.listAll());
        model.addAttribute("sys",systemDictionaryItemService.selectBySn("business"));
        return "index";
    }

    @RequestMapping("/appointment/save")
    @ResponseBody
    public JsonResult save(Appointment appointment){
        JsonResult jsonResult = new JsonResult();
        appointment.setStatus(StatusEnum.PEND.getValue());
        String date = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String s = RandomUtil.randomNumbers(5);
        appointment.setAno(date+s);
        appointment.setCreateTime(new Date());
        appointmentService.save(appointment);
        return jsonResult;
    }

}
